<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Stylist;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;

class StylistController extends Controller
{

    public function index(Request $request){
        $name = $request->input('name');
        $query = Stylist::orderBy('id','DESC');
        if ($name) {
            $query->where('name','like','%'.$name.'%');
        }
        $data['rows'] = $query->paginate(20);
        return view('admin/stylist/index',$data);
    }

    public function getInfo(Request $request){
        $id = intval($request->input('id'));
        if (!$id) {
            return $this->error('缺少参数');
        }
        $row = Stylist::find($id);
        if (!$row) {
            return $this->error('记录不存在');
        }
        return $this->response($row);
    }

    public function postCreate(Request $request){
        $name = $request->input('name');
        $introduce = $request->input('introduce');
        $level = $request->input('level');
        $photo = $request->input('photo');
        $row = new Stylist;
        $row->name =  $name;
        $row->introduce =  $introduce;
        $row->photo =  $photo;
        $row->level=$level;
        $row->save();
        return $this->response('ok');
    }

    public function postDelete(Request $request){
        $id = intval($request->input('id'));
        if (!$id) {
            return $this->error('缺少参数');
        }
        $rel = Stylist::where('id',$id)->delete();
        return $this->response('删除成功');
    }

    public function postEdit(Request $request){
        $id = intval($request->input('id'));
        $name = $request->input('name');
        $introduce = $request->input('introduce');
        $level = $request->input('level');
        $photo = $request->input('photo');
        $row = Stylist::find($id);
        if (!$row) {
            return $this->error('记录不存在');
        }
        $row->name =  $name;
        $row->photo =  $photo;
        $row->introduce =  $introduce;
        $row->level=$level;
        $row->save();
        return $this->response('ok');
    }
}
